import Vue from 'vue'
import VueRouter from 'vue-router'

import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: "/login",
    component: () => import('@/views/login'),
  },
  {
    path: "",
    component: () => import('@/views/layout'),
    children: [
      {
        path: "",
        component: () => import("@/views/home"),
        alias: "/home",
      },
      {
        path: "/vip",
        component: () => import("@/views/vip"),
      },
      {
        path: "/cart",
        component: () => import('@/views/cart'),
      },
      {
        path: "/my",
        component: () => import('@/views/my'),
      },
    ]
  },
  {
    path: "/details",
    component: () => import('@/views/details'),
  },
  {
    path: "/404",
    component: () => import("@/views/404"),
  },
  {
    path: "*",
    redirect: "/404",
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// const whiteList = ['/login', '/home'];
// router.beforeEach((to, from, next) => {
//   let token = window.localStorage.getItem('token');
//   if (token || whiteList.includes(to.path)) {
//     next();
//   } else {
//     next("/login");
//   }
// })
const token = store.state.login.user.nickname;
let newList = [
  {
    path: "/search",
    component: () => import('@/views/search'),
  },
]
if (token) {
  router.addRoutes(newList)
}


export default router
